package com.nic.view.widget;

import android.content.Context;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.nic.view.R;
import com.nic.view.util.DateTimeUtil;

import java.util.Calendar;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/5.
 */

public class CalendarPopupWindow extends CommonPopupWindow {
    private TextView tvBackToday;
    private MonthDateView monthDateView;
    private String selectedDay;//yyyy-MM-dd

    /**
     * 被选择的日期
     *
     * @return
     */
    public String getSelectedDay() {
        return selectedDay;
    }

    public void setSelectedDay(String selectedDay) {
        this.selectedDay = selectedDay;
    }

    public CalendarPopupWindow(Context c) {
        super(c, R.layout.k_popupwindow_canlder1, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        init();
    }

    private void init() {

        // 左右按钮
        ImageView iv_left = (ImageView) contentView.findViewById(R.id.iv_left);
        ImageView iv_right = (ImageView) contentView.findViewById(R.id.iv_right);
        // 日历
        monthDateView = (MonthDateView) contentView
                .findViewById(R.id.monthDateView);
        // 日期，周，返回到今天
        TextView tv_date = (TextView) contentView.findViewById(R.id.date_text);
        TextView tv_week = (TextView) contentView.findViewById(R.id.week_text);
        TextView tv_today = (TextView) contentView
                .findViewById(R.id.tv_today);
        monthDateView.setTextView(tv_date, tv_week);
        // 找控件
        TextView tv_out = (TextView) contentView.findViewById(R.id.tv_out);
        // 点击显示消失
        tv_out.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (getPopupWindow().isShowing()) {
                    getPopupWindow().dismiss();
                }
                return false;
            }
        });
        // 返回到今天
        tv_today.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                monthDateView.setTodayToView(DateTimeUtil.getSystemDataTime(), "yyyy-MM-dd HH:mm:ss");
            }
        });
        iv_left.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // 向左翻
                monthDateView.onLeftClick();
            }
        });
        iv_right.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // 向右
                monthDateView.onRightClick();
            }
        });
        monthDateView.setDateClick(new MonthDateView.DateClick() {
            @Override
            public void onClickOnDate() {
                if (monthDateView.getmSelDay() == 0) {
                    return;
                }
                if (getPopupWindow().isShowing()) {
                    getPopupWindow().dismiss();
                }
                String strDate = monthDateView.getSelDate();
                setSelectedDay(strDate);
                if (onDayClick != null) {
                    onDayClick.onDayClick(strDate);
                }
            }
        });
        tvBackToday = (TextView) contentView.findViewById(R.id.tv_back_today);
        tvBackToday.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (getPopupWindow().isShowing()) {
                    getPopupWindow().dismiss();
                }
                setSelectedDay(DateTimeUtil.getTodayDate());
                if (onDayClick != null) {
                    onDayClick.onDayClick(DateTimeUtil.getTodayDate());
                }
            }
        });
        setSelectedDay(DateTimeUtil.getTodayDate());

    }

    /**
     * 设置日期状态
     *
     * @param map
     */
    public void setKaoQinState(Map<String, String> map) {
        monthDateView.setStates(map);
    }


    public TextView getTvBackToday() {
        return tvBackToday;
    }

    public void showAtLocation(View v) {
        showAtLocation(v, Gravity.CENTER, 0, 0);
    }


    public void setSelectYearMonth(int year, int month, int day) {
        monthDateView.setSelectYearMonth(year, month, day);
        monthDateView.invalidate();
    }

    public void setCurrCalendar(Calendar currCalendar) {
        monthDateView.setCurrCalendar(currCalendar);
    }

    private OnDaySelectListener onDayClick;

    public void setOnDayClick(OnDaySelectListener onDayClick) {
        this.onDayClick = onDayClick;
    }

    public interface OnDaySelectListener {
        void onDayClick(String day);
    }

    private OnMonthSwitchListener onMonthSwitchListener;

    public void setOnMonthSwitchListener(OnMonthSwitchListener onMonthSwitchListener) {
        this.onMonthSwitchListener = onMonthSwitchListener;
    }

    public interface OnMonthSwitchListener {
        void onClick(String day);
    }
}
